<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/%E6%AD%A6%E6%B1%8932x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/%E6%AD%A6%E6%B1%8916x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib1/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib1/pace/pace-theme-center-simple.min.css">
  <script src="/lib1/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="前言由于之前项目是直接用vuecli构建的，使用webpack配置的情况较少，因此希望能够重新学习一下关于webpack方面的知识。这里主要还是以一个新手的角度来记述一下自己使用、学习webpack的过程。 webpack的定义：本质是一个module bundler(模块打包⼯具)，它会从⼊⼝模块出发，识别出源码中的模块化导⼊语句，递归地找出⼊⼝⽂件的所有依赖，将⼊⼝和其所有的依赖打包到⼀个单独">
<meta property="og:type" content="article">
<meta property="og:title" content="webpack基础配置">
<meta property="og:url" content="http://example.com/2021/03/21/webpack/webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE/index.html">
<meta property="og:site_name" content="Technological Blog">
<meta property="og:description" content="前言由于之前项目是直接用vuecli构建的，使用webpack配置的情况较少，因此希望能够重新学习一下关于webpack方面的知识。这里主要还是以一个新手的角度来记述一下自己使用、学习webpack的过程。 webpack的定义：本质是一个module bundler(模块打包⼯具)，它会从⼊⼝模块出发，识别出源码中的模块化导⼊语句，递归地找出⼊⼝⽂件的所有依赖，将⼊⼝和其所有的依赖打包到⼀个单独">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-03-21T04:18:39.000Z">
<meta property="article:modified_time" content="2021-11-03T08:23:03.084Z">
<meta property="article:author" content="Li Yudong">
<meta property="article:tag" content="webpack">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2021/03/21/webpack/webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>webpack基础配置 | Technological Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Technological Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">IT小白的成长之旅</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="tags fa-fw"></i>标签<span class="badge">18</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="th fa-fw"></i>分类<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="archive fa-fw"></i>归档<span class="badge">95</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/tiarmor1" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/03/21/webpack/webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/author.jpg">
      <meta itemprop="name" content="Li Yudong">
      <meta itemprop="description" content="">
    </span>
    
    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Technological Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          webpack基础配置
        </h1>
    
        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
    
              <time title="创建时间：2021-03-21 12:18:39" itemprop="dateCreated datePublished" datetime="2021-03-21T12:18:39+08:00">2021-03-21</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-11-03 16:23:03" itemprop="dateModified" datetime="2021-11-03T16:23:03+08:00">2021-11-03</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/webpack/" itemprop="url" rel="index"><span itemprop="name">webpack</span></a>
                </span>
            </span>
    
          
    
        </div>
      </header>
    
    
    
    
    <div class="post-body" itemprop="articleBody">
    
      
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>由于之前项目是直接用vuecli构建的，使用webpack配置的情况较少，因此希望能够重新学习一下关于webpack方面的知识。这里主要还是以一个新手的角度来记述一下自己使用、学习webpack的过程。</p>
<p>webpack的定义：本质是一个module bundler(模块打包⼯具)，它会从⼊⼝模块出发，识别出源码中的模块化导⼊语句，递归地找出⼊⼝⽂件的所有依赖，将⼊⼝和其所有的依赖打包到⼀个单独的⽂件中；是⼯程化、⾃动化思想在前端开发中的体现。</p>
<p>为什么会有webpack：因为前端语言逻辑发展很快，但浏览器对其的支持不尽如人意，例如模块化的共享。因此我们需要一个打包工具，将自己的文件解析成浏览器能够识别、运行的程序。</p>
<h1 id="webpack配置"><a href="#webpack配置" class="headerlink" title="webpack配置"></a>webpack配置</h1><p>默认配置：支持JS模块和JSON模块；支持CommonJS、ES Moudul e、AMD等模块类型。</p>
<p>1、webpack执行构建会找webpack.config.js的配置及文件，否则会走默认配置。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//webpack是基于nodeJS的，因此要准寻CommonJS的规范，使用module.export导出</span></span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">"path"</span>);</span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    <span class="comment">//上下文：项目打包的相对路径,很少去改变</span></span><br><span class="line">    context: process.cwd(),</span><br><span class="line">    <span class="comment">//入口 执行构建的入口 项目入口：支持字符串、数组、对象，</span></span><br><span class="line">    <span class="comment">//数组时会打包多个成为一个；对象才是多入口的,多入口一定要有多出口，将entry设置为对象，这样就会有多出口</span></span><br><span class="line">    entry: &#123;</span><br><span class="line">        index: <span class="string">"./src/index.js"</span>,</span><br><span class="line">        other: <span class="string">"./src/other.js"</span></span><br><span class="line">    &#125;</span><br><span class="line">    entry: [<span class="string">"./src/index.js"</span>, <span class="string">"./src/other.js"</span>]</span><br><span class="line">    entry: <span class="string">"./src/index.js"</span>,</span><br><span class="line">    <span class="comment">//出口：即打包后生成的bundle文件要放在哪里</span></span><br><span class="line">    output: &#123;</span><br><span class="line">        <span class="comment">//构建资源在哪，必须为绝对路径，因此用p  ath来处理</span></span><br><span class="line">        path: path.resolve(_dirname, <span class="string">"./dist"</span>), </span><br><span class="line">        <span class="comment">//构建的文件资源加什么 无论是多出口还是单出口，都推荐使用占位符</span></span><br><span class="line">        filename: <span class="string">"[name].js"</span>,</span><br><span class="line">        <span class="comment">//占位符有：hash、chunkhash、name、id</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">//构建模式： none production development 开发环境中，打包出的JS不会被压缩</span></span><br><span class="line">    <span class="comment">//设置mode可以自动触发webpack内置的函数，从而达到优化的效果</span></span><br><span class="line">    <span class="comment">//开发阶段的开启会有利于热更新的处理，识别哪个模块的变化</span></span><br><span class="line">    <span class="comment">//生产阶段的开启会有帮助于模块压缩，处理副作用等功能</span></span><br><span class="line">    mode: <span class="string">""</span>,</span><br><span class="line">    <span class="comment">//处理不认识的模块</span></span><br><span class="line">    <span class="built_in">module</span>: &#123;</span><br><span class="line">        rules: [</span><br><span class="line">            &#123;</span><br><span class="line">                test: <span class="regexp">/\.css$/</span>,</span><br><span class="line">                <span class="comment">//loader的执行顺序是从后往前</span></span><br><span class="line">                <span class="comment">//css-loader：把CSS模块的内容加入到JS的模块</span></span><br><span class="line">                <span class="comment">//style-loader：从JS中提取CSS的loader在HTML中创建</span></span><br><span class="line">                use: [<span class="string">"style-loader"</span>, <span class="string">"css-loader"</span>],</span><br><span class="line">            &#125;,</span><br><span class="line">        ],</span><br><span class="line">    &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>1 Chunk = 1Bundle；多个Chunk就会有多个Bundle，这样就是多入口与多出口的结构。只有entry为多键值对象时，才是多入口、多出口的文件。 chunk为代码块，bundle为资源文件，1个chunk可以由多个资源文件组成。</p>
<p>entry为数组时：webpack会⾃动⽣成另外⼀个⼊⼝模块，并将数组中的每个指定的模块加载进来，并将最后⼀个模块的module.exports作为⼊⼝模块的module.exports导出。</p>
<p>模块化开发：当前流行的单页面入口应用SPA，通过require、import引入其他的模块，并形成依赖，这样就不用进行多入口，但同样可以实现一样效果。</p>
<h2 id="loader"><a href="#loader" class="headerlink" title="loader"></a>loader</h2><p>Webpack 默认只⽀持.json 和 .js模块 不⽀持 不认识其他格式的模块。而此时就需要用到模块解析、模块转换器，⽤于把模块原内容按照需求转换成新内容，这些其他格式的模块处理就交给的是loader。⼀个loader只处理⼀件事情，且loader有执⾏顺序，执行顺序为从后往前。</p>
<p>常见的loader有：style-loader、css-loader、less-loader、sass-loader、ts-loader 将Ts转换成js、babel-loader转换ES6、7等js新特性语法、file-loader//处理图⽚、eslint-loader</p>
<h3 id="file-loader"><a href="#file-loader" class="headerlink" title="file-loader"></a>file-loader</h3><p>原理是把打包⼊⼝中识别出的资源模块，移动到输出⽬录，并且返回⼀个地址名称</p>
<p>所以我们什么时候⽤fifile-loader呢？场景：就是当我们需要模块，仅仅是从源代码挪移到打包⽬录，就可以使⽤file-loader来处理，txt，svg，csv，excel，图⽚资源啦等等</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>: &#123;</span><br><span class="line">    rules: [</span><br><span class="line">        &#123;</span><br><span class="line">            test: <span class="regexp">/\.(png|jpe?g|gif)$/</span>,</span><br><span class="line">            <span class="comment">//use使⽤⼀个loader可以⽤对象，字符串，两个loader需要⽤数组</span></span><br><span class="line">            use: &#123;</span><br><span class="line">                loader: <span class="string">"file-loader"</span>,</span><br><span class="line">                <span class="comment">// options额外的配置，⽐如资源名称</span></span><br><span class="line">                options: &#123;</span><br><span class="line">                    <span class="comment">// placeholder 占位符 [name]⽼资源模块的名称</span></span><br><span class="line">                    <span class="comment">// [ext]⽼资源模块的后缀</span></span><br><span class="line">                    <span class="comment">// https://webpack.js.org/loaders/fileloader#placeholders</span></span><br><span class="line">                    name: <span class="string">"[name]_[hash].[ext]"</span>,</span><br><span class="line">                    <span class="comment">//打包后的存放位置</span></span><br><span class="line">                    outputPath: <span class="string">"images/"</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<h3 id="url-loader"><a href="#url-loader" class="headerlink" title="url-loader"></a>url-loader</h3><p>其实是file-loader加强版本，url-loader内部使⽤了fifile-loader,所以可以处理fifile-loader所有的事情，但是遇到jpg格式的模块，会把该图⽚转换成base64格式字符串，并打包到js⾥。对⼩体积的图⽚⽐较合适，⼤图⽚不合适。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>: &#123;</span><br><span class="line">    rules: [</span><br><span class="line">        &#123;</span><br><span class="line">            test: <span class="regexp">/\.(png|jpe?g|gif)$/</span>,</span><br><span class="line">            use: &#123;</span><br><span class="line">                loader: <span class="string">"url-loader"</span>,</span><br><span class="line">                options: &#123;</span><br><span class="line">                    name: <span class="string">"[name]_[hash].[ext]"</span>,</span><br><span class="line">                    outputPath: <span class="string">"images/"</span>,</span><br><span class="line">                    <span class="comment">//⼩于2048，才转换成base64</span></span><br><span class="line">                    limit: <span class="number">2048</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<h3 id="CSS相关loader"><a href="#CSS相关loader" class="headerlink" title="CSS相关loader"></a>CSS相关loader</h3><p>Css-loader 分析css模块之间的关系，并合成⼀个css；</p>
<p>Style-loader 会把css-loader⽣成的内容，以style挂载到⻚⾯的heade部分；</p>
<p>less-load 把less语法转换成css</p>
<h2 id="moudle"><a href="#moudle" class="headerlink" title="moudle"></a>moudle</h2><p>模块，在 Webpack ⾥⼀切皆模块，⼀个模块对应着⼀个⽂件。Webpack 会从配置的 Entry 开始递归找出所有依赖的模块。当webpack处理到不认识的模块时，需要在webpack中的module处进⾏配置，当检测到是什么格式的模块，使⽤什么loader来处理。</p>
<h2 id="plugins"><a href="#plugins" class="headerlink" title="plugins"></a>plugins</h2><p>插件：对于webpack没有实现的功能，进行一个补充。作⽤于webpack打包整个过程，webpack的打包过程是有（⽣命周期概念）钩⼦，plugin 可以在webpack运⾏到某个阶段的时候，帮你做⼀些事情，类似于⽣命周期的概念，从而在 Webpack 构建流程中的特定时机注⼊扩展逻辑来改变构建结果或做你想要的事情。</p>
<p>loader是一个普通函数，plugin通常是一个类。 </p>
<h3 id="clean-webpack-plugin"><a href="#clean-webpack-plugin" class="headerlink" title="clean-webpack-plugin"></a><strong>clean-webpack-plugin</strong></h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; CleanWebpackPlugin &#125; = <span class="built_in">require</span>(<span class="string">"cleanwebpack-plugin"</span>);</span><br><span class="line">...</span><br><span class="line">plugins: [</span><br><span class="line"> <span class="keyword">new</span> CleanWebpackPlugin()</span><br><span class="line">]</span><br><span class="line"><span class="comment">//插件使用的方法比较简单，先引入再直接在plugins数组中添加上plugin即可。</span></span><br></pre></td></tr></table></figure>

<h3 id="HtmlWebpackPlugin"><a href="#HtmlWebpackPlugin" class="headerlink" title="HtmlWebpackPlugin"></a><strong>HtmlWebpackPlugin</strong></h3><p>htmlwebpackplugin会在打包结束后，⾃动⽣成⼀个html⽂件，并把打包⽣成的js模块引⼊到该html中。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">plugins: [</span><br><span class="line">    <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">        <span class="comment">//选择html模板模板⽂件路径，⽀持加载器，⽐如html!./index.html</span></span><br><span class="line">        template: <span class="string">"./src/index.html"</span>,</span><br><span class="line">        <span class="comment">//输出的 HTML ⽂件名，默认是 index.html, 也可以直接配置带有⼦⽬录。</span></span><br><span class="line">        filename: <span class="string">"index.html"</span>,</span><br><span class="line">    &#125;),</span><br><span class="line">],</span><br></pre></td></tr></table></figure>

<p><strong>HtmlWebpackPlugin</strong>的其他配置如下：</p>
<p>title: ⽤来⽣成⻚⾯的 title 元素</p>
<p>filename: 输出的 HTML ⽂件名，默认是 index.html, 也可以直接配置带有⼦⽬录。</p>
<p>template: 模板⽂件路径，⽀持加载器，⽐如html!./index.html</p>
<p>inject: true | ‘head’ | ‘body’ | false ,注⼊所有的资源到特定的 template 或者 templateContent 中，如果设置为true 或者 body，所有的 javascript 资源将被放置到 body 元素的底部，’head’ 将放置到 head 元素中。</p>
<p>favicon: 添加特定的 favicon 路径到输出的 HTML ⽂件中。</p>
<p>minify: {} | false , 传递 html-minifier 选项给 minify输出</p>
<p>hash: true | false, 如果为 true, 将添加⼀个唯⼀的webpack 编译 hash 到所有包含的脚本和 CSS ⽂件，对于解除cache 很有⽤。</p>
<p>cache: true | false，如果为 true, 这是默认值，仅仅在⽂件修改之后才会发布⽂件。</p>
<p>showErrors: true | false, 如果为 true, 这是默认值，错误信息会写⼊到 HTML ⻚⾯中</p>
<p>chunks: 允许只添加某些块 (⽐ 如，仅仅 unit test 块)</p>
<p>chunksSortMode: 允许控制块在添加到⻚⾯之前的排序⽅式，⽀持的值：’none’ | ‘default’ | {function}-default:’auto’</p>
<p>excludeChunks: 允许跳过某些块，(⽐如，跳过单元测试的块)</p>
<h2 id="devtool"><a href="#devtool" class="headerlink" title="devtool"></a>devtool</h2><p>当你开启development模式时，则会自动使用sourcemap；如果想要显式地开启sourcemap则可以加上devtool属性，设置为”source-map”。</p>
<p>eval:速度最快,使⽤eval包裹模块代码,</p>
<p>source-map： 产⽣.map⽂件</p>
<p>cheap:较快，不包含列信息</p>
<p>Module：第三⽅模块，包含loader的sourcemap（⽐如jsx to js ，</p>
<p>babel的sourcemap）</p>
<p>inline： 将.map作为DataURI嵌⼊，不单独⽣成.map⽂件</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">devtool:<span class="string">"cheap-module-eval-source-map"</span>,<span class="comment">// 开发环境配置</span></span><br><span class="line"><span class="comment">//线上不推荐开启</span></span><br><span class="line">devtool:<span class="string">"cheap-module-source-map"</span>, <span class="comment">// 线上⽣成配置</span></span><br></pre></td></tr></table></figure>

<h2 id="WebpackDevServer"><a href="#WebpackDevServer" class="headerlink" title="WebpackDevServer"></a><strong>WebpackDevServer</strong></h2><p><strong>WebpackDevServer</strong>是提升开发效率的利器，每次改完代码都需要重新打包⼀次，打开浏览器，刷新⼀次，很麻烦,我们可以安装使⽤webpackdevserver来改善这块的体验，实现本地开发服务的效果。</p>
<p>启动服务后，会发现dist⽬录没有了，这是因为devServer把打包后的模块不会放在dist⽬录下，⽽是放到内存中，从⽽提升速度。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//在webpack.config.js配置：</span></span><br><span class="line">devServer: &#123;</span><br><span class="line"> contentBase: <span class="string">"./dist"</span>,</span><br><span class="line"> <span class="comment">//是否自动打开浏览器窗口</span></span><br><span class="line"> open: <span class="literal">true</span>,</span><br><span class="line"> port: <span class="number">8080</span></span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<h3 id="本地mock"><a href="#本地mock" class="headerlink" title="本地mock"></a>本地mock</h3><p>前后端分离，前端会依赖后端的接口，一般后端会先给接口文档与接口联调日期。此时，前端就可以本地mock数据，而不打断自己的开发节奏。联调期间，前后端分离，直接获取数据会跨域，上线后我们使⽤nginx转发，开发期间，webpack就可以搞定这件事。</p>
<p>这个时候，我们使用express启动⼀个服务器，并mock⼀个接⼝：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建⼀个server.js 修改scripts "server":"node server.js"</span></span><br><span class="line"><span class="comment">//server.js</span></span><br><span class="line"><span class="keyword">const</span> express = <span class="built_in">require</span>(<span class="string">'express'</span>);</span><br><span class="line"><span class="keyword">const</span> app = express();</span><br><span class="line">app.get(<span class="string">'/api/info'</span>, (req,res)=&gt;&#123;</span><br><span class="line">    res.json(&#123;&#125;)</span><br><span class="line">&#125;);</span><br><span class="line">app.listen(<span class="string">'9092'</span>)</span><br><span class="line"><span class="comment">//node server.js</span></span><br></pre></td></tr></table></figure>

<p>用axios直接去请求端口会发生跨域问题：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//index.js</span></span><br><span class="line"><span class="keyword">import</span> axios <span class="keyword">from</span> <span class="string">'axios'</span></span><br><span class="line">axios.get(<span class="string">'http://localhost:9092/api/info'</span>).then(<span class="function"><span class="params">res</span>=&gt;</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(res)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>这个时候，我们可以直接用webpack.config.js来设置服务器代理，从而解决跨域问题，使用代理的方式将本地的请求代理到目标服务器生，从而打破同源策略。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">proxy: &#123;</span><br><span class="line">    <span class="string">"/api"</span>: &#123;</span><br><span class="line">        target: <span class="string">"http://localhost:9092"</span></span><br><span class="line">     &#125;</span><br><span class="line">&#125;,</span><br><span class="line"><span class="comment">//使用钩子函数快速地获取数据</span></span><br><span class="line">before()&#123;</span><br><span class="line">     app.get(<span class="string">"/api/mock.json"</span>, (req, res) =&gt; &#123;</span><br><span class="line">         res.json(&#123;</span><br><span class="line">             hello: <span class="string">"express"</span></span><br><span class="line">         &#125;);</span><br><span class="line">     &#125;);</span><br><span class="line">&#125;,</span><br><span class="line">after()&#123;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Hot-Module-Replacement热模块替换"><a href="#Hot-Module-Replacement热模块替换" class="headerlink" title="Hot Module Replacement热模块替换"></a><strong>Hot Module Replacement</strong>热模块替换</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">devServer: &#123;</span><br><span class="line">    contentBase: <span class="string">"./dist"</span>,</span><br><span class="line">    open: <span class="literal">true</span>,</span><br><span class="line">    hot:<span class="literal">true</span>,</span><br><span class="line">    <span class="comment">//即便HMR不⽣效，浏览器也不⾃动刷新，就开启hotOnly</span></span><br><span class="line">    hotOnly:<span class="literal">true</span></span><br><span class="line"> &#125;</span><br><span class="line"><span class="comment">//在配置头文件引入webpack，并在插件配置处添加</span></span><br></pre></td></tr></table></figure>

<p>注意启动HMR后，css抽离会不⽣效，因此需要使用css-loader，还有不⽀持contenthash，chunkhash。</p>
<p>contenthash、chunkhash、hash三个占位符的区别：</p>
<p>Hash：构建依赖，每次构建时都会生成一个新的Hash;</p>
<p>chunkHash：当我们有多入口时，我们可以使用chunkHash，别的入口文件发生改变，不会影响其他入口文件；</p>
<p>contentHash：当在JS里面有CSS的话，CSS一般使用contentHsh，因为content跟内容有关系。修改了JS的话，即使没有改变CSS,用hash或者chunkHash均会引起变更，但用contentHash则会进行对比，不会变更。</p>
<h3 id="JS模块的HMR"><a href="#JS模块的HMR" class="headerlink" title="JS模块的HMR"></a>JS模块的HMR</h3><p>需要使⽤module.hot.accept来观察模块更新 从⽽更新，当module.hot开启时，则手动地进行。react hot loader可以实时地调整react组件，同样Vue-loader自身就支持HMR。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> counter <span class="keyword">from</span> <span class="string">"./counter"</span>;</span><br><span class="line"><span class="keyword">import</span> number <span class="keyword">from</span> <span class="string">"./number"</span>;</span><br><span class="line">counter();</span><br><span class="line">number();</span><br><span class="line"><span class="keyword">if</span> (<span class="built_in">module</span>.hot) &#123;</span><br><span class="line"> <span class="built_in">module</span>.hot.accept(<span class="string">"./b"</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">     <span class="built_in">document</span>.body.removeChild(<span class="built_in">document</span>.getElementById(<span class="string">"number"</span>));</span><br><span class="line">     number();</span><br><span class="line"> &#125;);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Babel"><a href="#Babel" class="headerlink" title="Babel"></a>Babel</h2><p>Babel是JavaScript编译器，能将ES6代码转换成ES5代码，让我们开发过程中放⼼使⽤JS新特性⽽不⽤担⼼兼容性问题。并且还可以通过插件机制根据需求灵活的扩展。Babel在执⾏编译的过程中，会从项⽬根⽬录下的.babelrc JSON⽂件中读取配置。没有该⽂件会从loader的options地⽅读取配置。</p>
<p>env是babel7之后推⾏的预设插件，会根据各大浏览器支持的程度来进行语法的转换。</p>
<p>babel-loader是webpack 与 babel的通信桥梁，不会做把es6转成es5的⼯作，这部分⼯作需要⽤到@babel/preset-env来做。@babel/preset-env⾥包含了es，6，7，8转es5的转换规则</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line"> test: <span class="regexp">/\.js$/</span>,</span><br><span class="line"> exclude: <span class="regexp">/node_modules/</span>,</span><br><span class="line"> use: &#123;</span><br><span class="line">     loader: <span class="string">"babel-loader"</span>,</span><br><span class="line">     options: &#123;</span><br><span class="line">         <span class="comment">//语法转换插件:preset-env</span></span><br><span class="line">         presets: [<span class="string">"@babel/preset-env"</span>],</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="polyfill"><a href="#polyfill" class="headerlink" title="polyfill"></a>polyfill</h3><p>通过上⾯的⼏步 还不够，默认的Babel只⽀持let等⼀些基础的特性转换，Promise等⼀些还有转换过来，这时候需要借助@babel/polyfifill，把es的新特性都装进来，来弥补低版本浏览器中缺失的特性。polyfill垫片 其实是ES6+的ECMA规范库，本身并不进行转换，还是交给@babel/preset-env来进行转换。</p>
<p>如果以全局变量的方式注入，往往会造成全局变量的污染，会发现打包的体积⼤了很多，这是因为polyfifill默认会把所有特性注⼊进来，假如我想我⽤到的es6+，才会注⼊，没⽤到的不注⼊，从⽽减少打包的体积，这就是所谓的按需加载。垫片的含义就是：缺少什么才垫上什么。</p>
<p>polyfill对全局变量有污染问题，但在正常做开发时Promise本来就挂在window上，因此没有区别。但做UI库、组件库、工具库时会有问题。</p>
<p>这个时候我们可以修改Webpack.config.js：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">options: &#123;</span><br><span class="line">    presets: [</span><br><span class="line">        [</span><br><span class="line">            <span class="string">"@babel/preset-env"</span>,</span><br><span class="line">            &#123;</span><br><span class="line">                targets: &#123;</span><br><span class="line">                edge: <span class="string">"17"</span>,</span><br><span class="line">                firefox: <span class="string">"60"</span>,</span><br><span class="line">                chrome: <span class="string">"67"</span>,</span><br><span class="line">                safari: <span class="string">"11.1"</span></span><br><span class="line">                &#125;,</span><br><span class="line">                corejs: <span class="number">2</span>,<span class="comment">//新版本需要指定核⼼库版本</span></span><br><span class="line">                useBuiltIns: <span class="string">"usage"</span><span class="comment">//按需注⼊</span></span><br><span class="line">            &#125;</span><br><span class="line">        ]</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>useBuiltIns选项是babel 7的新功能，这个选项告诉babel如何配置@babel/polyfill。 它有三个参数可以使⽤： 1、entry: 需要在webpack的⼊⼝⽂件⾥import “@babel/polyfill”⼀次。babel会根据你的使⽤情况导⼊垫⽚，没有使⽤的功能不会被导⼊相应的垫⽚。</p>
<p>2、usage: 不需要import，全⾃动检测，但是要安装@babel/polyfill。（试验阶段） </p>
<p>3、false: 如果你import”@babel/polyfill”，它不会排除掉没有使⽤的垫⽚，程序体积会庞⼤。(不推荐)</p>
<p>请注意： usage 的⾏为类似 babel-transform-runtime，不会造成全局污染，因此也会不会对类似 Array.prototype.includes() 进⾏polyfill。</p>
<h3 id="babelrc"><a href="#babelrc" class="headerlink" title="babelrc"></a>babelrc</h3><p>为避免Webpack.config.js中配置过多，我们将babel的配置单独提取出来。新建.babelrc⽂件，把options部分移⼊到该⽂件中，就可以了</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line"> presets: [</span><br><span class="line">     [</span><br><span class="line">         <span class="string">"@babel/preset-env"</span>,</span><br><span class="line">         &#123;</span><br><span class="line">             targets: &#123;</span><br><span class="line">             edge: <span class="string">"17"</span>,</span><br><span class="line">             firefox: <span class="string">"60"</span>,</span><br><span class="line">             chrome: <span class="string">"67"</span>,</span><br><span class="line">             safari: <span class="string">"11.1"</span></span><br><span class="line">             &#125;,</span><br><span class="line">             corejs: <span class="number">2</span>, <span class="comment">//新版本需要指定核⼼库版本</span></span><br><span class="line">             useBuiltIns: <span class="string">"usage"</span> <span class="comment">//按需注⼊</span></span><br><span class="line">         &#125;</span><br><span class="line">     ]</span><br><span class="line"> ]</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//webpack.config.js</span></span><br><span class="line">&#123;</span><br><span class="line"> test: <span class="regexp">/\.js$/</span>,</span><br><span class="line"> exclude: <span class="regexp">/node_modules/</span>,</span><br><span class="line"> loader: <span class="string">"babel-loader"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="React"><a href="#React" class="headerlink" title="React"></a>React</h3><p>如何让babel支持JSX的语法，babel的插件其实分为两种，一种为语法转换，另一种为语法的解析。安装babel与react转换的插件： @babel/preset-react。</p>
<p>在babelrc⽂件⾥添加：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="string">"presets"</span>: [</span><br><span class="line">        [</span><br><span class="line">            <span class="string">"@babel/preset-env"</span>,</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="string">"targets"</span>: &#123;</span><br><span class="line">                    <span class="string">"edge"</span>: <span class="string">"17"</span>,</span><br><span class="line">                    <span class="string">"firefox"</span>: <span class="string">"60"</span>,</span><br><span class="line">                    <span class="string">"chrome"</span>: <span class="string">"67"</span>,</span><br><span class="line">                    <span class="string">"safari"</span>: <span class="string">"11.1"</span>,</span><br><span class="line">                    <span class="string">"Android"</span>:<span class="string">"6.0"</span></span><br><span class="line">                &#125;,</span><br><span class="line">                <span class="string">"useBuiltIns"</span>: <span class="string">"usage"</span>, <span class="comment">//按需注⼊</span></span><br><span class="line">            &#125;</span><br><span class="line">        ],</span><br><span class="line">        <span class="string">"@babel/preset-react"</span></span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="babel-plugin-transform-runtime"><a href="#babel-plugin-transform-runtime" class="headerlink" title="@babel/plugin-transform-runtime"></a><strong>@babel/plugin-transform-runtime</strong></h3><p>当我们开发的是组件库，⼯具库这些场景的时候，polyfill就不适合了，因为polyfifill是注⼊到全局变量，window下的，会污染全局环境，所以推荐闭包⽅式：@babel/plugin-transform-runtime，它不会造成全局污染。会使用替换的方式。例如本来有promise，这时候会创建_promise，用来替换原来语法的promise。</p>

    </div>
    
    
    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>
        <div class="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/alipay.png" alt="Li Yudong 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Li Yudong
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://example.com/2021/03/21/webpack/webpack%E5%9F%BA%E7%A1%80%E9%85%8D%E7%BD%AE/" title="webpack基础配置">http://example.com/2021/03/21/webpack/webpack基础配置/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

    
      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/webpack/" rel="tag"># webpack</a>
          </div>
    
        

    
        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/03/17/vue%E6%BA%90%E7%A0%81/vue%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90(%E4%B8%89)/" rel="prev" title="vue源码分析(三)">
      <i class="fa fa-chevron-left"></i> vue源码分析(三)
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/03/27/webpack/webpack%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" rel="next" title="webpack性能优化">
      webpack性能优化 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#webpack配置"><span class="nav-number">2.</span> <span class="nav-text">webpack配置</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#loader"><span class="nav-number">2.1.</span> <span class="nav-text">loader</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#file-loader"><span class="nav-number">2.1.1.</span> <span class="nav-text">file-loader</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#url-loader"><span class="nav-number">2.1.2.</span> <span class="nav-text">url-loader</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#CSS相关loader"><span class="nav-number">2.1.3.</span> <span class="nav-text">CSS相关loader</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#moudle"><span class="nav-number">2.2.</span> <span class="nav-text">moudle</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#plugins"><span class="nav-number">2.3.</span> <span class="nav-text">plugins</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#clean-webpack-plugin"><span class="nav-number">2.3.1.</span> <span class="nav-text">clean-webpack-plugin</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#HtmlWebpackPlugin"><span class="nav-number">2.3.2.</span> <span class="nav-text">HtmlWebpackPlugin</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#devtool"><span class="nav-number">2.4.</span> <span class="nav-text">devtool</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#WebpackDevServer"><span class="nav-number">2.5.</span> <span class="nav-text">WebpackDevServer</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#本地mock"><span class="nav-number">2.5.1.</span> <span class="nav-text">本地mock</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Hot-Module-Replacement热模块替换"><span class="nav-number">2.6.</span> <span class="nav-text">Hot Module Replacement热模块替换</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#JS模块的HMR"><span class="nav-number">2.6.1.</span> <span class="nav-text">JS模块的HMR</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Babel"><span class="nav-number">2.7.</span> <span class="nav-text">Babel</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#polyfill"><span class="nav-number">2.7.1.</span> <span class="nav-text">polyfill</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#babelrc"><span class="nav-number">2.7.2.</span> <span class="nav-text">babelrc</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#React"><span class="nav-number">2.7.3.</span> <span class="nav-text">React</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#babel-plugin-transform-runtime"><span class="nav-number">2.7.4.</span> <span class="nav-text">@babel&#x2F;plugin-transform-runtime</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Li Yudong"
      src="/images/author.jpg">
  <p class="site-author-name" itemprop="name">Li Yudong</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">95</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/tiarmor1" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tiarmor1" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1157019137@qq.com" title="E-Mail → mailto:1157019137@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2020 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Li Yudong</span>
</div>

        








      </div>
    </footer>
  </div>

  
  
  <script color='0,0,0' opacity='0.5' zIndex='-1' count='150' src="/lib1/canvas-nest/canvas-nest.min.js"></script>
  <script src="/lib1/anime.min.js"></script>
  <script src="/lib1/velocity/velocity.min.js"></script>
  <script src="/lib1/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  
   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/fireworks.js"></script>
  
</body>
</html>

